/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lardocelar.core;

import com.sun.media.protocol.http.DataSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;


/**
 *
 * @author LeandroL
 */
@SuppressWarnings("restriction")
public class ServicoSistema {

    public static String Ouvir() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        return bufferedReader.readLine();
    }

    public static void Falar(String fala) throws MalformedURLException, IOException, LineUnavailableException, UnsupportedAudioFileException, NoPlayerException {
        URL url = new URL("https://translate.google.com.br/translate_tts?ie=UTF-8&q=Teste&tl=pt&total=1&idx=1&textlen=5&client=t");
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.addRequestProperty("User-Agent", "Mozilla/4.76");
        InputStream audioSrc = urlConn.getInputStream();
        Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
        Format input2 = new AudioFormat(AudioFormat.MPEG);
        Format output = new AudioFormat(AudioFormat.LINEAR);
        PlugInManager.addPlugIn(
            "com.sun.media.codec.audio.mp3.JavaDecoder",
            new Format[]{input1, input2},
            new Format[]{output},
            PlugInManager.CODEC
        );
        MediaLocator ml = new MediaLocator(url);        
        DataSource ds = new DataSource();
        ds.setLocator(ml);
        ds.connect();
        //Player player = Manager.createPlayer((DataSource)audioSrc); //Manager.createPlayer(new MediaLocator(url));
        //player.start();
        /*
        String word="car";
        word=java.net.URLEncoder.encode(fala, "UTF-8");
        URL url = new URL("https://translate.google.com.br/translate_tts?ie=UTF-8&q=Teste&tl=pt&total=1&idx=1&textlen=5&client=t");
        Clip clip = AudioSystem.getClip();
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.addRequestProperty("User-Agent", "Mozilla/4.76");
        InputStream audioSrc = urlConn.getInputStream();
        AudioInputStream ais = AudioSystem.getAudioInputStream(audioSrc);
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
        */
        /*
        URL url = new URL("https://translate.google.com.br/translate_tts?ie=UTF-8&q=Teste&tl=pt&total=1&idx=1&textlen=5&client=t");
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        urlConn.addRequestProperty("User-Agent", "Mozilla/4.76");
        InputStream audioSrc = urlConn.getInputStream();
        DataInputStream read = new DataInputStream(audioSrc);
        AudioStream as = new AudioStream(read);
        AudioPlayer.player.start(as);
        AudioPlayer.player.stop(as);
        */
        /*
        AudioInputStream din = null;
        try {
            //AudioInputStream in = AudioSystem.getAudioInputStream(new URL("https://translate.google.com.br/translate_tts?ie=UTF-8&q=Teste&tl=pt&total=1&idx=1&textlen=5&client=t"));
            AudioInputStream in = AudioSystem.getAudioInputStream(new URL("http://www.howjsay.com/mp3/"+ fala +".mp3"));
            AudioFormat baseFormat = in.getFormat();
            AudioFormat decodedFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
                    baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
                    false);
            din = AudioSystem.getAudioInputStream(decodedFormat, in);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
            if (line != null) {
                line.open(decodedFormat);
                byte[] data = new byte[4096];
                // Start
                line.start();

                int nBytesRead;
                while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
                    line.write(data, 0, nBytesRead);
                }
                // Stop
                line.drain();
                line.stop();
                line.close();
                din.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (din != null) {
                try {
                    din.close();
                } catch (IOException e) {
                }
            }
        }
                */
        
    }

    public static void Falar(Object fala) {
        System.out.println(fala);
    }
}
